package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.File;
import com.elite.entity.FileGoods;
import com.elite.entity.Goods;
import com.elite.service.FileService;
import com.elite.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("/search")
public class SearchController {


    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FileService fileService;

    private static final Logger LOGGER = Logger.getLogger(String.valueOf(SearchController.class));

    @RequestMapping(value = "/goodsByGoodsName",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String search(String goodsName){
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        List<Goods> goodses = null;
        try{
            goodses = goodsService.searchGoodsByGoodsName(goodsName);
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        if (goodses != null) {
            for (Goods goods:goodses){
                if (goods.getGoodsSize() > 0){
                    List<FileGoods> fileGoodses = null;
                    try{
                        fileGoodses = goodsService.selectByGoodsId(goods.getGoodsId());
                    } catch (NullPointerException e){
                        e.printStackTrace();
                    }
                    int index = 0;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        index = (int) (Math.random()*fileGoodses.size());
                    }
                    FileGoods fileGoods = null;
                    if (fileGoodses != null && fileGoodses.size() != 0) {
                        fileGoods = fileGoodses.get(index);
                    }
                    File file = null;
                    if (fileGoods != null) {
                        file = fileService.selectByPrimaryKey(fileGoods.getFileId());
                    }
                    if (file != null){
                        Map<String,Object> map = new HashMap<String, Object>();
                        map.put("goodsName",goods.getGoodsName());
                        map.put("goodsPrice",goods.getGoodsPrice());
                        map.put("goodsCity",goods.getGoodsCity());
                        map.put("goodsSchool",goods.getGoodsSchool());
                        map.put("goodsUnit",goods.getGoodsUnit());
                        map.put("goodsId",goods.getGoodsId());
                        map.put("fileUrl",file.getFileUrl());
                        mapList.add(map);
                    } else {
                        Map<String,Object> map = new HashMap<String, Object>();
                        map.put("goodsName",goods.getGoodsName());
                        map.put("goodsPrice",goods.getGoodsPrice());
                        map.put("goodsCity",goods.getGoodsCity());
                        map.put("goodsSchool",goods.getGoodsSchool());
                        map.put("goodsUnit",goods.getGoodsUnit());
                        map.put("goodsId",goods.getGoodsId());
                        map.put("fileUrl","");
                        mapList.add(map);
                    }
                }
            }
        }

        return JSON.toJSON(mapList).toString();
    }


    @RequestMapping(value = "/recommend",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String recommend(){
        List<Goods> goodses = goodsService.selectGoods();
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 6; i++) {
            int index = (int) (Math.random()*goodses.size());
            Goods goods = goodses.get(index);
            if (goods.getGoodsSize() > 0){
                List<FileGoods> fileGoodses = null;
                try{
                    fileGoodses = goodsService.selectByGoodsId(goods.getGoodsId());
                } catch (NullPointerException e){
                    e.printStackTrace();
                }
                int index1 = 0;
                if (fileGoodses != null && fileGoodses.size() != 0) {
                    index1 = (int) (Math.random()*fileGoodses.size());
                }
                FileGoods fileGoods = null;
                if (fileGoodses != null && fileGoodses.size() != 0) {
                    fileGoods = fileGoodses.get(index1);
                }
                File file = null;
                if (fileGoods != null) {
                    file = fileService.selectByPrimaryKey(fileGoods.getFileId());
                }
                if (file != null){
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("goodsName",goods.getGoodsName());
                    map.put("goodsCity",goods.getGoodsCity());
                    map.put("goodsId",goods.getGoodsId());
                    map.put("fileUrl",file.getFileUrl());
                    mapList.add(map);
                } else {
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("goodsName",goods.getGoodsName());
                    map.put("goodsCity",goods.getGoodsCity());
                    map.put("goodsId",goods.getGoodsId());
                    map.put("fileUrl","");
                    mapList.add(map);
                }
            }
        }
        return JSON.toJSON(mapList).toString();
    }


}
